<template>
  <el-dialog title="分配角色" :visible="showRoleDialog" @close="btnCancel">
    =={{ roleIds }}
    <!-- el-checkbox-group选中的是 当前用户所拥有的角色  需要绑定 当前用户拥有的角色-->
    <el-checkbox-group v-model="roleIds">
      <el-checkbox
        v-for="(item, index) in list"
        :key="index"
        :label="item.id"
        >{{ item.name }}</el-checkbox
      >
      <!-- 选项 -->
    </el-checkbox-group>
    <el-row slot="footer" type="flex" justify="center">
      <el-col :span="6">
        <el-button type="primary" size="small" @click="btnOK">确定</el-button>
        <el-button size="small" @click="btnCancel">取消</el-button>
      </el-col>
    </el-row>
  </el-dialog>
</template>
<script>
import { getRoleListApi } from "@/api/setting";
import { getUserDetailByIdApi } from "@/api/user";
import { assignRolesApi } from "@/api/employees";

export default {
  data() {
    return {
      list: [], // 角色列表
      roleIds: []
    };
  },
  props: {
    showRoleDialog: {
      type: Boolean,
      default: false
    },
    // 用户的id 用来查询当前用户的角色信息
    userId: {
      type: String,
      default: null
    }
  },
  created() {
    this.getRoleList();
  },
  methods: {
    async getRoleList() {
      const res = await getRoleListApi();
      console.log(res.data.rows, 333445);
      this.list = res.data.rows;
    },
    //  获取当前角色
    async getUserDetailById(userId) {
      //  **获取用户的当前角色**
      const res = await getUserDetailByIdApi(userId).catch(err => err);
      console.log(res, 11);
      this.roleIds = res.data.roleIds;
    },
    btnCancel() {
      //this.showRoleDialog = false;
      this.$emit("update:showRoleDialog", false);
      this.roleIds = [];
    },
    //  保存
    async btnOK() {
      const res = await assignRolesApi({
        id: this.userId,
        roleIds: this.roleIds
      });
      //   关闭窗体
      this.btnCancel();
    }
  }
};
</script>
